package com.puhui.lib.widgets

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import com.blankj.utilcode.util.BarUtils
import com.puhui.lib.R
import com.puhui.lib.utils.setClick
import kotlin.math.min

class BaseAppBarLayout @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null
) : ConstraintLayout(context, attrs) {

    init {
        initView(attrs)
    }

    private var mTvTitle: TextView? = null

    private fun initView(attrs: AttributeSet?) {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.BaseAppBarLayout)
        val title = typedArray.getString(R.styleable.BaseAppBarLayout_textTitle)
        val titleTextColor = typedArray.getColor(R.styleable.BaseAppBarLayout_textColorTitle, Color.BLACK)
        val backIcon = typedArray.getResourceId(R.styleable.BaseAppBarLayout_iconBack, R.mipmap.back_left)
        val needPadding = typedArray.getBoolean(R.styleable.BaseAppBarLayout_needPadding, false)
        val maxPadding = typedArray.getDimensionPixelSize(R.styleable.BaseAppBarLayout_maxPadding, Int.MAX_VALUE)
        val textSizeTitle = typedArray.getDimensionPixelSize(R.styleable.BaseAppBarLayout_textSizeTitle, -1)
        val drawable = typedArray.getDrawable(R.styleable.BaseAppBarLayout_drawableRightTitle)
        val drawablePaddingTitle = typedArray.getDimension(R.styleable.BaseAppBarLayout_drawablePaddingTitle, 0f)
        val hideIconBack = typedArray.getBoolean(R.styleable.BaseAppBarLayout_hideIconBack, false)
        typedArray.recycle()

        val root: View = LayoutInflater.from(context).inflate(R.layout.layout_appbar_constraint, this)

        mTvTitle = root.findViewById(R.id.titleTv)
        if (textSizeTitle != -1) {
            mTvTitle?.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSizeTitle.toFloat())
        }
        mTvTitle?.text = title
        mTvTitle?.setTextColor(titleTextColor)
        mTvTitle?.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null)
        mTvTitle?.compoundDrawablePadding = drawablePaddingTitle.toInt()

        val ivBack = findViewById<ImageView>(R.id.backImage)
        if (hideIconBack) {
            ivBack.visibility = GONE
        } else {
            ivBack.visibility = VISIBLE
            ivBack.setImageResource(backIcon)
            if (context is Activity) {
                ivBack.setClick { (context as Activity).onBackPressed() }
            }
        }

        if (needPadding) {
            post {
                val statusBarHeight = BarUtils.getStatusBarHeight()
                val padding = min(statusBarHeight, maxPadding)
                val paddingTop = paddingTop + padding
                setPadding(paddingLeft, paddingTop, paddingRight, paddingBottom)

                val layoutParams = this.layoutParams
                layoutParams.height = layoutParams.height + paddingTop
            }
        }
    }

    fun setTitleRightDrawableRes(@DrawableRes resId: Int) {
        mTvTitle?.setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(context, resId), null)
    }

    fun setTitle(charSequence: CharSequence?) {
        if (null != charSequence) {
            mTvTitle?.text = charSequence
        }
    }

    fun setTitleColor(@ColorInt colorInt: Int) {
        mTvTitle?.setTextColor(colorInt)
    }

    fun setTitleClick(block: () -> Unit) {
        mTvTitle?.setClick { block() }
    }
}